package service

import (
	"context"
	"errors"
	__ "shop-microservices/basic/goods_proto"
	"shop-microservices/handler/dao"
	"strconv"
)

// 商品详情
func (s *Server) GoodsDetail(_ context.Context, in *__.GoodsDetailReq) (*__.GoodsDetailResp, error) {
	goodsDetail, err := dao.GoodsDetail(in)
	if err != nil {
		return nil, err
	}
	detail := &__.GoodsDetail{
		GoodsId:     goodsDetail.Id,
		GoodsName:   goodsDetail.GoodsName,
		GoodsPrice:  goodsDetail.GoodsPrice,
		GoodsDesc:   goodsDetail.GoodsDesc,
		ImageUrl:    goodsDetail.ImageUrl,
		SliderImage: goodsDetail.SliderImage,
	}
	return &__.GoodsDetailResp{GoodsDetail: detail}, nil
}

// 店铺详情
func (s *Server) ShopDetail(_ context.Context, in *__.ShopDetailReq) (*__.ShopDetailResp, error) {

	shopDetail, err := dao.ShopDetail(in)
	if err != nil {
		return nil, err
	}

	pdShopDetail := &__.ShopDetail{
		GoodsId:     shopDetail.Id,
		GoodsName:   shopDetail.GoodsName,
		GoodsPrice:  float32(shopDetail.GoodsPrice),
		GoodsDesc:   shopDetail.GoodsDesc,
		ShopName:    shopDetail.ShopName,
		ShopDesc:    shopDetail.ShopDesc,
		ShopLogo:    shopDetail.ShopLogo,
		ShopPhone:   shopDetail.ShopPhone,
		ShopAddress: shopDetail.ShopAddress,
		ShopStatus:  string(shopDetail.ShopStatus),
		ImageUrl:    shopDetail.ImageUrl,
		SliderImage: shopDetail.SliderImage,
	}

	return &__.ShopDetailResp{
		ShopDetail: pdShopDetail,
	}, nil
}

// 商店推荐
func (s *Server) GoodsPush(_ context.Context, in *__.GoodsPushReq) (*__.GoodsPushResp, error) {
	push, err := dao.GoodsPush(in)
	if err != nil {
		return nil, err
	}
	return &__.GoodsPushResp{List: push}, nil
}

// 弹窗广告
func (s *Server) PopUpAd(_ context.Context, in *__.PopUpAdReq) (*__.PopUpAdResp, error) {
	var sd dao.AdService
	a, err := sd.GetAdvertisementList(strconv.FormatInt(in.UserId, 10))
	if err != nil {
		return nil, errors.New("查询失败" + err.Error())
	}
	return &__.PopUpAdResp{
		Id:      int64(a.Id),
		Title:   a.Title,
		Content: a.Content,
		ImgUrl:  a.ImgUrl,
		LinkUrl: a.LinkUrl,
	}, nil
}
